<script lang="ts" setup>
import type { UpdataType } from '../../types';

import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';

import { message, Select, SelectOption } from 'ant-design-vue';

import { useVbenForm, z } from '#/adapter/form';
import { getAdminRoles } from '#/api/admin/roles';
import {
  getAdminUsersInfo,
  postAdminUsers,
  putAdminUsers,
} from '#/api/admin/user';

const roles: any = ref([]);
const getRoles = async () => {
  const {
    data: { list },
  } = await getAdminRoles();
  roles.value = list;
};
const [Form, formApi] = useVbenForm({
  handleSubmit: onSubmit,
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'user_login',
      label: $t('admin.UserName'),
      rules: 'required',
    },
    {
      component: 'InputPassword',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'user_pass',
      label: $t('admin.password'),
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'user_email',
      label: $t('admin.email'),
      rules: z.string().email($t('admin.pleaseEnterValidEmail'),),
    },
    {
      component: 'Select',
      fieldName: 'role_ids',
      label: $t('admin.role'),
      rules: 'required',
    },
  ],
  showDefaultActions: false,
});
const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  onCancel() {
    formModalApi.close();
  },
  onConfirm: async () => {
    await formApi.validateAndSubmitForm();
  },
  async onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: UpdataType = formModalApi.getData();
      if (values && values?.id) {
        const {
          data: { role_ids },
        } = await getAdminUsersInfo(values.id);
        values.role_ids = role_ids;
        formApi.setValues(values);
      }
    }
  },
});

async function onSubmit(values: any) {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
    formModalApi.close();
  }
  if (values.id) {
    const { msg } = await putAdminUsers(values.id, values);
    showMsg(msg);
  } else {
    const { msg } = await postAdminUsers(values);
    showMsg(msg);
  }
}

onMounted(async () => {
  await getRoles();
});
</script>
<template>
  <Modal class="w-3/5">
    <Form>
      <template #role_ids="slotProps">
        <Select v-bind="slotProps" mode="multiple" style="width: 100%">
          <SelectOption v-for="item in roles" :key="item.id" :value="item.id">
            {{ item.name }}
          </SelectOption>
        </Select>
      </template>
    </Form>
  </Modal>
</template>

<style lang="scss" scoped></style>
